package JUnitTests;

import static org.junit.Assert.*;

import java.sql.Date;
import java.sql.SQLException;
import java.util.Calendar;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import DataStorageClasses.*;
import Database.DatabaseConnector;

public class BusinessConnectorTests 
{
	private static DatabaseConnector dbConnector;
	private static String testerBus = "TesterBusiness";
	private static String rightPassword = "rightPassword";
	private static String busEmail = "email";
	private static String emailPass = "email password";
	
	@BeforeClass static public void initialize()
	{
		dbConnector = new DatabaseConnector();
		dbConnector.connect();
		
		dbConnector.addBusiness(testerBus, rightPassword, null, null, null, busEmail, null, null, null, null, emailPass);
	}
	
	@AfterClass static public void destroy()
	{
		dbConnector.removeBusiness(testerBus, rightPassword);
		dbConnector.disconnect();
	}
	
	@Test public void testAddGetBusiness()
	{
		Business bus = dbConnector.getProfile(testerBus);
		assertNotNull("DB Connector returned a null business", bus);
		
		String busName = bus.getBusname();
		assertArrayEquals("Did not return the right business",testerBus.toCharArray(), busName.toCharArray());
	}
	
	@Test public void testRemoveBusiness()
	{
		String testerBus = "TesterBusiness1234";
		String otherBus = "wrongBus";
		String rightPassword = "password";
		String wrongPassword = "wrongPassword";
		
		boolean removed = dbConnector.removeBusiness(testerBus, wrongPassword);
		assertFalse("removeBusiness returned true with wrong password", removed);
		Business business = dbConnector.getProfile(testerBus);
		assertNull("business was removed with wrong password", business);
		
		removed = dbConnector.removeBusiness(otherBus, rightPassword);
		assertFalse("removeBusiness returned true with wrong business name",removed);
		business = dbConnector.getProfile(testerBus);
		assertNull("business was removed with wrong business name", business);
		
		removed = dbConnector.removeBusiness(testerBus, rightPassword);
		assertFalse("remove Business returned false with correct business name and password",removed);
		business = dbConnector.getProfile(testerBus);
		assertNull("business was not removed with correcdt business name and password", business);
	}
	
	@Test public void testSignIn()
	{
		String wrongPassword = "wrongPassword";
		
		try {
			Business bus = dbConnector.signIn(testerBus, wrongPassword);
			assertNull("got business with wrong password",bus);
			
			bus = dbConnector.signIn(testerBus, rightPassword);
			assertNotNull("null returned with right password", bus);
			assertArrayEquals("business signed in for and business returned not the same", testerBus.toCharArray(),
					bus.getBusname().toCharArray());
		} catch (SQLException e) {
			fail("sql exception thrown");
			e.printStackTrace();
		}
	}
	
	@Test public void testEditBusiness()
	{
		String newNum = "1234";
		
		boolean changed = dbConnector.editBusiness(testerBus, null, null, null, null, null, null, null, null, null);
		assertFalse(changed);
		
		changed = dbConnector.editBusiness(testerBus, null, newNum, null, null, null, null, null, null, null);
		assertTrue(changed);
		
		Business bus = dbConnector.getProfile(testerBus);
		assertArrayEquals(newNum.toCharArray(), bus.getBusNum().toCharArray());
	}
	
	@Test public void getInfo()
	{
		EmailInfo info = dbConnector.getEmailInfo(testerBus);
		String email = info.getEmail();
		String emailpass = info.getPassword();
		
		assertArrayEquals(email.toCharArray(), busEmail.toCharArray());
		assertArrayEquals(emailpass.toCharArray(), emailPass.toCharArray());
	}
	
	@Test public void getTotalAmountOwing()
	{	
		Calendar calendar = Calendar.getInstance();
		calendar.set(2010, 2, 3);
		Date finish1 = new Date(calendar.getTimeInMillis());
		calendar.set(2010, 5, 8);
		Date finish2 = new Date(calendar.getTimeInMillis());
		calendar.set(2010, 9, 3);
		Date finish3 = new Date(calendar.getTimeInMillis());
		
		calendar.set(2010,1,1);
		Date low = new Date(calendar.getTimeInMillis());
		calendar.set(2010,6, 1);
		Date high = new Date(calendar.getTimeInMillis());
		
		Client client = dbConnector.addClient("Brian", "McMahon", null, null, null, null, null, null, null, null, testerBus);
		Client client2 = dbConnector.addClient("first", "last", null, null, null, null, null, null, null, null, testerBus);
		Job job = dbConnector.addNewJob(client.getClientId(), 0, 100, 25, null, null, null, finish1, null, null, null, testerBus);
		Job job2 = dbConnector.addNewJob(client.getClientId(), 0, 67, 32.3, null, null, null, finish2, null, null, null, testerBus);
		Job job3 = dbConnector.addNewJob(client2.getClientId(), 0, 290, 89, null, null, null, finish3, null, null, null, testerBus);
		
		System.out.println(dbConnector.getTotalWages(testerBus, low, high));
		
		dbConnector.removeJob(job.getJobID());
		dbConnector.removeJob(job2.getJobID());
		dbConnector.removeJob(job3.getJobID());
		dbConnector.removeClient(client.getClientId());
		dbConnector.removeClient(client2.getClientId());
	}
	
	@Test public void getTotalIncome()
	{
		dbConnector.getTotalMoneyMade(testerBus, new Date(0), new Date(0));
	}
	
}
